package com.fan.teamer.model.dao;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Named;

import org.hibernate.SessionFactory;

import com.fan.teamer.model.domain.Role;

@Named("roleDao")
public class RoleDaoImpl implements RoleDao {

	private SessionFactory sessionFactory;

	/**
	 * Get Hibernate Session Factory
	 * 
	 * @return SessionFactory - Hibernate Session Factory
	 */
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	/**
	 * Set Hibernate Session Factory
	 * 
	 * @param SessionFactory
	 *            - Hibernate Session Factory
	 */
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public Role getRole(String email) {
		Role role = (Role) sessionFactory.getCurrentSession().get(Role.class,
				email);
		return role;
	}

	@SuppressWarnings("unchecked")
	public List<Role> getAllRoles() {
		return sessionFactory.getCurrentSession().createCriteria(Role.class)
				.list();
	}

	public int getSize() {
		return getAllRoles().size();
	}

	public void addRole(Role role) {
		sessionFactory.getCurrentSession().save(role);

	}

	public void updateStock(Role role) {
		sessionFactory.getCurrentSession().update(role);
	}

	public void deleteRole(Role role) {
		sessionFactory.getCurrentSession().delete(role);
	}

	public boolean hasRole(String email) {
		if (getRole(email) == null)
			return false;
		else
			return true;
	}

	public List<String> getColumnNames() {
		List<String> columns = new ArrayList<String>();
		columns.add("ID");
		columns.add("Email");
		columns.add("ROLE_NAME");
		columns.add("ROLE_GROUP");
		return columns;
	}

	public String getUserGroup(String email) {
		if (hasRole(email)) {
			return getRole(email).getRoleGroup();
		} else {
			return null;
		}
	}

}
